import { CopyControllerBase } from "./CopyControllerBase";
import { RobFlagEnterWin } from "../robFlag/RobFlagEnterWin";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { SystemSetter } from "../../system/SystemSetter";
import { RobFlagCopyInfoPanel } from "../robFlag/RobFlagCopyInfoPanel";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SRobFlagLeftTime } from "../../../../../Message/Public/SRobFlagLeftTime";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { DataEvent } from "../../../events/DataEvent";
import { GameProxy } from "../../../mvc/GameProxy";
import { RobFlagSignUpType } from "../robFlag/data/RobFlagSignUpType";
import { Global } from "../../../../../com/gengine/global/Global";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Language } from "../../../../../extend/language/Language";
import { SPlayerResultList } from "../../../../../Message/Public/SPlayerResultList";
import { RobFlagRankPanel } from "../robFlag/RobFlagRankPanel";
import { SRobFlagBattleInfo } from "../../../../../Message/Public/SRobFlagBattleInfo";
import { EEntityAction } from "../../../../../Message/Public/EEntityAction";
import { ObjCreate } from "../../common/ObjCreate";
import { ResFileConst } from "../../../resource/ResFileConst";
import { LayerManager } from "../../../manager/LayerManager";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { TimerAlertWin } from "../../common/alertwins/TimerAlertWin";
import { SRobFlagForceInfo } from "../../../../../Message/Public/SRobFlagForceInfo";
import { HintBtnBaseView } from "../../mapUI/HintBtnBaseView";
import { GameMapUtil } from "../../../scene/map/GameMapUtil";
import { MsgManager } from "../../../manager/MsgManager";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
type int = number;
//class RobFlagController
    
    export  class RobFlagController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */createEnterWin(): void
        {
            if (this._enterWin != null) 
            {
                return;
            }
            this._enterWin = new RobFlagEnterWin();
            this._enterWin.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.enterWinAddToStageHandler);
            return;
        }

        /* internal  */enterWinAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            if (cache.robFlag.curSignUpType < 0) 
            {
                this.checkAndUpdateSignUpStatus();
            }
            if (this._enterWin != null) 
            {
                this._enterWin.updateMyHornor(cache.role.money.robFlagHonour);
            }
            this._enterWin.updateExchangeList(cache.robFlag.getExchangeList());
            return;
        }

        /* internal  */checkAndUpdateSignUpStatus(): void
        {
            this.setSignUpType(cache.robFlag.getInitSignUpType());
            return;
        }

        /* internal  */setSignUpType(arg1: int): void
        {
            if (this._enterWin == null) 
            {
                return;
            }
            cache.robFlag.curSignUpType = arg1;
            this._enterWin.updateSignUpStatus(arg1);
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            this._originalShowFabao = SystemSetter.currentSetter.hideFabaoEffect;
            if (this._infoPanel == null) 
            {
                this._infoPanel = new RobFlagCopyInfoPanel();
            }
            if (cache.robFlag.copyInfo != null) 
            {
                this._infoPanel.updateProgress(cache.robFlag.copyInfo.teamOneScore, cache.robFlag.copyInfo.teamTwoScore);
            }
            this._infoPanel.updatePosition();
            this._infoPanel.show();
            this._infoPanel.updateLeftTime(cache.robFlag.leftTime);
            if (!(this._enterWin == null) && !this._enterWin.isHide) 
            {
                this._enterWin.hide();
            }
            this.checkAndUpdateSignUpStatus();
            return;
        }

        /* internal  */copyLeftTimeHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobFlagLeftTime;
            if (loc1 == null || loc1.leftDt == null) 
            {
                return;
            }
            cache.robFlag.endDate = loc1.leftDt;
            if (this._infoPanel != null) 
            {
                this._infoPanel.updateLeftTime(cache.robFlag.leftTime);
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.RobFlagShowModuleReq, this.showEntranceHandler);
            Dispatcher.addEventListener(EventName.RobFlagSingleSignUpReq, this.singleSignUpReqHandler);
            Dispatcher.addEventListener(EventName.RobFlagTeamSignUpReq, this.teamSignUpReqHandler);
            Dispatcher.addEventListener(EventName.RobFlagShowExchangeModule, this.showExchangeHandler);
            Dispatcher.addEventListener(EventName.RobFlagEnterCopyReq, this.enterCopyReqHandler);
            Dispatcher.addEventListener(EventName.RobFlagSignUpLeaveReq, this.cancelSignUpReqHandler);
            NetDispatcher.addCmdListener(ServerCommand.RobFlagSignUpFail, this.signUpFailHandler);
            NetDispatcher.addCmdListener(ServerCommand.RobFlagContributionUpdate, this.myCountributionUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.CrossGroup_PlayerInfoChange, this.teamChangeHandler);
            Dispatcher.addEventListener(EventName.RobFlagUserEnterCopyTimeOut, this.enterCopyTimeOutHandler);
            Dispatcher.addEventListener(EventName.RoleEntityActionChange, this.roleEntityActionChangeHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagOpen, this.openSuccessHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagClose, this.closeSuccessHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagSignUpSuccess, this.serverSignUpSuccessHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagCancelSignUp, this.serverCancelSignUpHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagMatchSuccess, this.matchSuccessHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagForceInfo, this.forceInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagBattleInfo, this.copyInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagLeftTime, this.copyLeftTimeHandler);
            return;
        }

        protected /* override */ addListener(): void
        {
            super.addListener();
            Dispatcher.addEventListener(EventName.StageResize, this.stageResizeHandler);
            Dispatcher.addEventListener(EventName.RobFlagLeaveCopyReq, this.leaveCopyReqHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobFlagResultInfo, this.rankResultHandler);
            Dispatcher.addEventListener(EventName.RobFlagAutoReliveReq, this.autoReliveReqHandler);
            return;
        }

        protected /* override */ removeListener(): void
        {
            super.removeListener();
            Dispatcher.removeEventListener(EventName.StageResize, this.stageResizeHandler);
            Dispatcher.removeEventListener(EventName.RobFlagLeaveCopyReq, this.leaveCopyReqHandler);
            NetDispatcher.removeCmdListener(EPublicCommand._ECmdPublicRobFlagResultInfo, this.rankResultHandler);
            Dispatcher.removeEventListener(EventName.RobFlagAutoReliveReq, this.autoReliveReqHandler);
            if (this._btnDrowFlag != null) 
            {
                this._btnDrowFlag.removeEventListener(flash.events.MouseEvent.CLICK, this.dropFlagReqHandler);
                this._btnDrowFlag = null;
            }
            return;
        }

        /* internal  */enterCopyTimeOutHandler(arg1: DataEvent): void
        {
            this.checkAndUpdateSignUpStatus();
            GameProxy.copy.signUpRobFlagCopy(RobFlagSignUpType.cancel);
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            cache.robFlag.rankInfo = null;
            if (!(this._infoPanel == null) && !this._infoPanel.isHide) 
            {
                this._infoPanel.hide();
                this._infoPanel.dispose(true);
                this._infoPanel = null;
            }
            if (!(this._rankPanel == null) && !this._rankPanel.isHide) 
            {
                this._rankPanel.hide();
                this._rankPanel.dispose(true);
                this._rankPanel = null;
            }
            return;
        }

        /* internal  */serverSignUpSuccessHandler(arg1: Object): void
        {
            var loc1=RobFlagSignUpType.single;
            if (cache.crossGroup.isInGroup) 
            {
                loc1 = RobFlagSignUpType.team;
            }
            this.createEnterWin();
            this._enterWin.show();
            this.setSignUpType(loc1);
            return;
        }

        /* internal  */serverCancelSignUpHandler(arg1: Object): void
        {
            var loc1=RobFlagSignUpType.initSingle;
            if (cache.crossGroup.isInGroup) 
            {
                loc1 = RobFlagSignUpType.initTeam;
            }
            this.setSignUpType(loc1);
            return;
        }

        /* internal  */stageResizeHandler(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=0;
            if (this._infoPanel != null) 
            {
                this._infoPanel.updatePosition();
            }
            if (this._btnDrowFlag != null) 
            {
                loc1 = Global.stage.stageWidth;
                loc2 = Global.stage.stageHeight;
                this._btnDrowFlag.x = loc1 < 1060 ? loc1 - 200 : 860;
                this._btnDrowFlag.y = loc2 < 710 ? loc2 - 80 : 630;
            }
            return;
        }

        /* internal  */leaveCopyReqHandler(arg1: DataEvent): void
        {
            var loc1=Boolean(arg1.data);
            if (loc1 || !(cache.robFlag.rankInfo == null)) 
            {
                GameProxy.copy.leaveRobFlagCopy();
                return;
            }
            Alert.show(Language.getString(80528), null, Alert.OK | Alert.CANCEL, null, this.closeLeaveCopyHandler);
            return;
        }

        /* internal  */closeLeaveCopyHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                return;
            }
            GameProxy.copy.leaveRobFlagCopy();
            return;
        }

        /* internal  */rankResultHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPlayerResultList;
            cache.robFlag.rankInfo = loc1;
            if (this._rankPanel == null) 
            {
                this._rankPanel = new RobFlagRankPanel();
            }
            this._rankPanel.show();
            this._rankPanel.updateData(cache.robFlag.allRankData, cache.robFlag.redRankData, cache.robFlag.blueRankData);
            return;
        }

        /* internal  */autoReliveReqHandler(arg1: DataEvent): void
        {
            GameProxy.copy.moveToPassPoint(0);
            return;
        }

        /* internal  */copyInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobFlagBattleInfo;
            cache.robFlag.copyInfo = loc1;
            if (this._infoPanel != null) 
            {
                this._infoPanel.updateProgress(loc1.teamOneScore, loc1.teamTwoScore);
            }
            return;
        }

        /* internal  */roleEntityActionChangeHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            var loc2=cache.role.lastEntityAction;
            if (loc2 & EEntityAction._EEntityActionRobFlag) 
            {
                this._actionTimerId = flash.utils.setTimeout(this.showDropFlagTimer, 10000);
            }
            else if (loc1 & EEntityAction._EEntityActionRobFlag) 
            {
                this.enableDropRobFlag = false;
                if (this._actionTimerId > 0) 
                {
                    flash.utils.clearTimeout(this._actionTimerId);
                    this._actionTimerId = -1;
                }
            }
            return;
        }

        /* internal  */showDropFlagTimer(): void
        {
            this._actionTimerId = -1;
            this.enableDropRobFlag = true;
            return;
        }

        /* internal  */set enableDropRobFlag(arg1: Boolean)
        {
            if (arg1) 
            {
                if (this._btnDrowFlag == null) 
                {
                    this._btnDrowFlag = ObjCreate.createGLoadedButton(ResFileConst.RobFlagDropFlagBtn, 0, 0, 200, 80, LayerManager.uiLayer);
                    this._btnDrowFlag.addEventListener(flash.events.MouseEvent.CLICK, this.dropFlagReqHandler);
                    this.stageResizeHandler(null);
                }
                LayerManager.uiLayer.addChild(this._btnDrowFlag);
            }
            else 
            {
                DisplayUtil.removeMe(this._btnDrowFlag);
            }
            return;
        }

        /* internal  */dropFlagReqHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            GameProxy.copy.dropRobFlag();
            return;
        }

        /* internal  */matchSuccessHandler(arg1: MessageBlock): void
        {
            if (this._enterWin == null) 
            {
                this.createEnterWin();
            }
            this._enterWin.show();
            Alert.timerOut = 30;
            Alert.timerNO = true;
            Alert.mode = Alert.Mode_NoneNotModal;
            Alert.alertWinRenderer = TimerAlertWin;
            Alert.buttonWidth = 70;
            Alert.show(Language.getString(80520), null, Alert.OK | Alert.CANCEL, null, this.matchAlertCloseHandler);
            this.setSignUpType(RobFlagSignUpType.matchSuccess);
            return;
        }

        /* internal  */matchAlertCloseHandler(arg1: int): void
        {
            if (arg1 == Alert.CANCEL) 
            {
                return;
            }
            if (arg1 != Alert.OK) 
            {
                return;
            }
            GameProxy.copy.enterRobFlagCopy();
            return;
        }

        /* internal  */signUpSuccessHandler(arg1: Object): void
        {
            var loc1=int(arg1);
            if (loc1 == RobFlagSignUpType.cancel) 
            {
                loc1 = cache.robFlag.getInitSignUpType();
            }
            this.setSignUpType(loc1);
            return;
        }

        /* internal  */signUpFailHandler(arg1: Object): void
        {
            var loc1=int(arg1);
            if (!(loc1 == RobFlagSignUpType.single) && !(loc1 == RobFlagSignUpType.team)) 
            {
                return;
            }
            this.checkAndUpdateSignUpStatus();
            if (this._enterWin) 
            {
                this._enterWin.setCanCancel(true);
            }
            GameProxy.copy.signUpRobFlagCopy(RobFlagSignUpType.cancel);
            return;
        }

        /* internal  */forceInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobFlagForceInfo;
            cache.robFlag.forceInfo = loc1;
            return;
        }

        /* internal  */myCountributionUpdateHandler(arg1: Object): void
        {
            if (this._enterWin == null) 
            {
                return;
            }
            this._enterWin.updateMyHornor(cache.role.money.robFlagHonour);
            return;
        }

        /* internal  */teamChangeHandler(arg1: Object): void
        {
            var loc1=cache.robFlag.curSignUpType;
            var loc2=cache.robFlag.getInitSignUpType();
            if (loc1 != RobFlagSignUpType.team) 
            {
                if (loc1 != RobFlagSignUpType.single) 
                {
                    if (cache.robFlag.curSignUpType != RobFlagSignUpType.matchSuccess) 
                    {
                        this.setSignUpType(loc2);
                    }
                }
                else if (loc2 == RobFlagSignUpType.initTeam) 
                {
                    GameProxy.copy.signUpRobFlagCopy(RobFlagSignUpType.cancel);
                }
            }
            else if (loc2 == RobFlagSignUpType.initSingle) 
            {
                GameProxy.copy.signUpRobFlagCopy(RobFlagSignUpType.cancel);
            }
            return;
        }

        /* internal  */closeSuccessHandler(arg1: MessageBlock): void
        {
            this.checkAndUpdateSignUpStatus();
            if (this._enterBtn != null) 
            {
                this._enterBtn.hide();
                this._enterBtn.dispose(true);
                this._enterBtn = null;
            }
            return;
        }

        /* internal  */openSuccessHandler(arg1: MessageBlock): void
        {
            if (cache.role.roleInfo.level < 70) 
            {
                return;
            }
            if (this._enterBtn == null) 
            {
                this._enterBtn = new HintBtnBaseView(ResFileConst.RobFlagHintBtn, EventName.RobFlagShowModuleReq);
            }
            this._enterBtn.show();
            this._enterBtn.showEffect(true);
            return;
        }

        /* internal  */singleSignUpReqHandler(arg1: DataEvent): void
        {
            if (!GameMapUtil.curMapState.isCrossMainCity) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80531));
                return;
            }
            if (cache.crossGroup.isInGroup) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80518));
                return;
            }
            GameProxy.copy.signUpRobFlagCopy(RobFlagSignUpType.single);
            return;
        }

        /* internal  */teamSignUpReqHandler(arg1: DataEvent): void
        {
            if (!GameMapUtil.curMapState.isCrossMainCity) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80531));
                return;
            }
            if (!cache.crossGroup.isInGroup || !cache.crossGroup.isCaptain) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80519));
                return;
            }
            GameProxy.copy.signUpRobFlagCopy(RobFlagSignUpType.team);
            return;
        }

        /* internal  */showExchangeHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */enterCopyReqHandler(arg1: DataEvent): void
        {
            GameProxy.copy.enterRobFlagCopy();
            return;
        }

        /* internal  */cancelSignUpReqHandler(arg1: DataEvent): void
        {
            if (cache.crossGroup.isInGroup && !cache.crossGroup.isCaptain) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80536));
                return;
            }
            GameProxy.copy.signUpRobFlagCopy(RobFlagSignUpType.cancel);
            return;
        }

        /* internal  */showEntranceHandler(arg1: DataEvent): void
        {
            if (this._enterWin == null) 
            {
                this.createEnterWin();
            }
            this._enterWin.show();
            return;
        }

        /* internal */ /* var */_enterWin: RobFlagEnterWin;

        /* internal */ /* var */_infoPanel: RobFlagCopyInfoPanel;

        /* internal */ /* var */_rankPanel: RobFlagRankPanel;

        /* internal */ /* var */_enterBtn: HintBtnBaseView;

        /* internal */ /* var */_btnDrowFlag: GLoadedButton;

        /* internal */ /* var */_originalShowFabao: Boolean;

        /* internal */ /* var */_actionTimerId: int=-1;
    }
